import { useUserStore } from '@/stores'

// 判断是否有这个别名，如果有就留下，没有就去掉这个元素节点
const hasPermission = (value, el = false) => {
  const userStore = useUserStore()
  if (!Array.isArray(value)) {
    throw new Error(`需要配置权限，例如v-permission="['getStatistics3,GET']"`)
  }
  const hasAuth = value.findIndex((v) => userStore.ruleNames.includes(v)) !== -1
  if (el && !hasAuth) {
    el.parentNode && el.parentNode.removeChild(el)
  }
  return hasAuth
}
// 自定义指令
export default {
  install(app) {
    // console.log(app)
    app.directive('permission', {
      mounted(el, binding) {
        // console.log(el, binding)
        hasPermission(binding.value, el)
      },
    })
  },
}
